mc.game.army = function(){};

mc.game.army.refresh = function(index){
	mc.cmd.get_right().empty();
	mc.game.current_leader = false;
	mc.leader_list = false;
	mc.cmd.get_left_inner(2).empty();
	mc.game.menu.select(index);
}
mc.game.army.refresh_delay = function(){
	window.setTimeout(function(){
		mc.game.army.refresh(0);
		mc.tape.put_console("очистили");
	},1000);
}
mc.game.army.show = function(){
	mc.game.army.show_for(mc.game.army);
}
mc.game.army.show_for = function(context){
	if(mc.leader_list){
		mc.game.army.draw.call(context);
	}else{
		mc.game.load_start();
		$.when(
			mc.request(
				"game",
				{cmd: "leaders.load"},
				function(data){
					mc.leader_list = new mc.game.leader_list(data);
					mc.game.army.draw.call(this);
				},
				context
			)
		).then(mc.game.load_end);
	}
}
mc.game.army.find_current_leader = function(){
	if(mc.game.current_leader){
		return $("#leader_list").find('div.leader[entry="'+mc.game.current_leader+'"]');
	}else{
		return $("#leader_list").find('div.leader:eq(0)');
	}
}
mc.game.army.draw = function(){
	var to = mc.cmd.get_right();
	var api = to.data("jsp");
	if(api){
		api.destroy();
	}
	to.empty();
	
	var data = mc.leader_list.leaders();
	var o = $.tmpl("leader_list",data).appendTo(to).filter("div.scrolled");
	var i = o.find(">div");
	mc.game.calc_width(i);

	api = o.jScrollPane(scroll_pane).data('jsp');
	i.delegate("div.leader","click",this.leader_click);

	var current = mc.game.army.find_current_leader();
	api.scrollToElement(current);
	this.leader_click.call(current);
}
mc.game.army.leader_select = function(obj){
	mc.game.army.find_current_leader().removeClass("bordered");

	var entry = $(obj).addClass("bordered").attr("entry");
	mc.game.current_leader = entry;
	
	var to_leader = mc.cmd.get_left_inner(2).empty();
	mc.game.army.leader_draw(entry,to_leader);
	
	return entry;
}

mc.game.army.leader_click = function(event){
	var entry = mc.game.army.leader_select(this);
	var to_unit = mc.cmd.get_right_inner(1).empty();
	var to_item = mc.cmd.get_right_inner(2).empty();

	mc.game.load_start();
	$.when(
		mc.game.army.show_unit(entry,to_unit),
		mc.game.army.show_item(entry,to_item)
	).then(mc.game.load_end);
	
	if(event){
		event.stopImmediatePropagation();
	}
}
mc.game.army.leader_draw = function(entry,to){
	var l = mc.leader_list.leader(entry);
	$.tmpl("menu_context",{l:l,p:mc.game.get_proto("leaders")[l.proto_id]}).appendTo(to);
}
mc.game.army.show_unit = function(entry,to){
	return $.when(
			mc.leader_list.ask_unit(entry)
		).then(function(){
			mc.game.army.draw_unit(entry,to);
		});
}
mc.game.army.draw_unit = function(entry,to){
	var o = $.tmpl("army_unit_list",mc.leader_list.units(entry)).appendTo(to);
	var i = o.find(">div");
	mc.game.calc_width(i);
	o.jScrollPane(scroll_pane);
}
mc.game.army.show_item = function(entry,to){
	return $.when(
			mc.leader_list.ask_item(entry)
		).then(function(){
			mc.game.army.draw_item(entry,to);
		});
}
mc.game.army.draw_item = function(entry,to){
	var o = $.tmpl("army_item_list",mc.leader_list.items(entry)).appendTo(to);
	var i = o.find(">div");
	mc.game.calc_width(i);
	o.jScrollPane(scroll_pane);
}
